import { deleteRightTodo, deleteRightItem, spliceGetDataTodo, getSaleNum, getValueArray, setEmptyArray, setArrayValue, setExchangeValue, spliceGetDataItem } from '@/views/sales/common/TableSaleMethods'
export default ({
  state: {
    saleData: [],
    giftData: [],
    GetsaleData: [],
    GetgiftData: [],
    form: {
      name: '',
      intro: '',
      date: [],
      grade: [],
      term: [1],
      num: [],
      con: '',
      remark: '',
      reduce: '',
      add: 0,
      giftCheck: false,
      reduceCheck: false,
      addCheck: false,
      isGiftDisable: false,
      isReduceDisable: true,
      isAddDisable: true,
      isParcel: false
    }
  },
  getters: {
    getFullSubGiftSaleGiftNum (state) {
      return getSaleNum(state.GetgiftData, { childName: 'productSkuList' })
    },
    getFullsubGiftSaleNum (state) {
      return getSaleNum(state.GetsaleData, { childName: 'productSkuList' })
    },
    fullGiftgrade (state, getters) {
      let newarray = []
      for (let item of state.form.grade) {
        for (let todo of getters.storeTypeData) {
          if (item == todo.value) {
            newarray.push(todo.text)
          }
        }
      }
      return newarray.join(',')
    },
    hasGift (state) {
      let word = ''
      if (state.form.giftCheck) {
        word = '有'
      } else {
        word = '无'
      }
      return word
    }
  },
  mutations: {
    setFullSubGiftSaleEmpty (state) {
      state.GetsaleData = []
      state.saleData = []
      state.form = {
        name: '',
        intro: '',
        date: [],
        grade: [],
        term: [1],
        num: [],
        con: '',
        remark: '',
        reduce: '',
        add: 0,
        giftCheck: false,
        reduceCheck: false,
        addCheck: false,
        isGiftDisable: false,
        isReduceDisable: true,
        isAddDisable: true,
        isParcel: false
      }
    },
    setFullSubGiftSaleGiftEmpty (state) {
      state.GetgiftData = []
      state.GetsaleData = []
      state.giftData = []
      state.saleData = []
      state.form = {
        name: '',
        intro: '',
        date: [],
        grade: [],
        term: [1],
        num: [],
        con: '',
        remark: '',
        reduce: '',
        add: 0,
        giftCheck: false,
        reduceCheck: false,
        addCheck: false,
        isGiftDisable: false,
        isReduceDisable: true,
        isAddDisable: true,
        isParcel: false
      }
    },
    getfullSubGiftData (state, type) {
      let getdata = JSON.parse(JSON.stringify(state.saleData))
      // state.GetsaleData = JSON.parse(JSON.stringify(state.saleData));
      getValueArray(getdata, {
        index: type,
        childName: 'productSkuList',
        status: false
      })
      setEmptyArray(getdata, {
        index: type,
        childName: 'productSkuList'
      })
      setArrayValue(getdata, {
        index: type,
        val1: false,
        childName: 'productSkuList'
      })
      if (!state.GetsaleData.length) { //首次为空
        state.GetsaleData = getdata
      } else { // 防止每次更新的淹没其他的
        //把没有的第一层数据存在，GetsaleData里面
        //把没有的第二层的数据存在, GetsaleData里面
        spliceGetDataItem(getdata, state.GetsaleData)
        spliceGetDataTodo(getdata, state.GetsaleData)
      }
      if (type !== 'buttonChecked') {
        setExchangeValue(state.saleData, { index1: 'checked', index: 'buttonChecked', childName: 'productSkuList' })
      }
    },
    getfullSubGiftPreventData (state, type) {
      let getdata = JSON.parse(JSON.stringify(state.giftData))
      // state.GetsaleData = JSON.parse(JSON.stringify(state.saleData));
      getValueArray(getdata, {
        index: type,
        childName: 'productSkuList',
        status: false
      })
      setEmptyArray(getdata, {
        index: type,
        childName: 'productSkuList'
      })
      setArrayValue(getdata, {
        index: type,
        val1: false,
        childName: 'productSkuList'
      })
      if (!state.GetgiftData.length) { //首次为空
        state.GetgiftData = getdata
      } else { // 防止每次更新的淹没其他的
        //把没有的第一层数据存在，GetgiftData里面
        //把没有的第二层的数据存在, GetgiftData里面
        spliceGetDataItem(getdata, state.GetgiftData)
        spliceGetDataTodo(getdata, state.GetgiftData)
      }
      if (type !== 'buttonChecked') {
        setExchangeValue(state.giftData, { index1: 'checked', index: 'buttonChecked', childName: 'productSkuList' })
      }
    },
    deletefullSubGiftPrevent (state, object) {
      if (object.todo && !object.todo.buttonChecked || object.item && !object.item.buttonChecked) {
        if (object.todo) { //没子集
          deleteRightTodo(object.item, object.todo, state.GetgiftData)
        } else { //有子集
          deleteRightItem(object.item, state.GetgiftData)
        }
      }
    },
    deletefullSubGift (state, object) {
      if (object.todo && !object.todo.buttonChecked || object.item && !object.item.buttonChecked) {
        if (object.todo) { //没子集
          deleteRightTodo(object.item, object.todo, state.GetsaleData)
        } else { //有子集
          deleteRightItem(object.item, state.GetsaleData)
        }
      }
    },
    test (state, object) {
      if (object.status && !object.value) {
        return object.callback(new Error('请输入减金额'))
      } else if (object.status && !/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(object.value)) {
        return object.callback(new Error('优惠条件必须为正数'))
      } else if (object.status && object.value / state.form.con >= 0.8) {
        return object.callback(new Error('减金额最大为满金额的80%'))
      } else {
        object.callback()
      }
    },
    testCon (state, object) {
      if (!object.value) {
        return object.callback(new Error('请输入优惠条件'))
      } else if (!/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(object.value)) {
        return object.callback(new Error('优惠条件必须为正数'))
      } else {
        object.callback()
      }
    },
    addtest (state, object) {
      if (object.status && !object.value) {
        return object.callback(new Error('请输入加价购金额'))
      } else if (object.status && !/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(object.value)) {
        return object.callback(new Error('优惠条件必须为正数'))
      } else {
        object.callback()
      }
    },
    setGift (state, param) {
      state.form.giftCheck = param
    },
    setParcel (state, param) {
      state.form.isParcel = param
    },
    getsubSaleData (state) {
      state.GetsaleData = JSON.parse(JSON.stringify(state.saleData))
    },
    GetsubgiftData (state) {
      state.GetgiftData = JSON.parse(JSON.stringify(state.giftData))
    }
  }
})
